package com.ezmobo.service;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ezmobo.core.database.ContextHolder;
import com.ezmobo.dao.AdminUser.AdminUserDO;
import com.ezmobo.dao.AdminUser.AdminUserMapper;
import com.ezmobo.dao.Student.StudentDO;
import com.ezmobo.dao.Student.StudentMapper;
import com.ezmobo.dao.adminlog.Adminlog;
import com.ezmobo.dao.adminlog.AdminlogMapper;

@Service
public class TestDB {
	private static Logger logger = LoggerFactory.getLogger(TestDB.class);
	@Autowired
	private AdminUserMapper adminDao;
	@Autowired
	private StudentMapper studentDao;
	@Autowired
	private Test1 test1;
	@Autowired
	private AdminlogMapper logDao;

	private int number = 0;

	@PostConstruct
	public void init() {

		// ContextHolder.setMimiDB();
		List<AdminUserDO> list = adminDao.getAdminUsers();

		try {
			// add();
		} catch (Exception e) {
			e.printStackTrace();
		}

		logger.info("1231231311111");
	}

	/**
	 * 
	 * 指定回滚
	 * 
	 * @Transactional(rollbackFor=Exception.class)
	 * 
	 * 											指定不回滚
	 * @Transactional(noRollbackFor=Exception.class)
	 * 
	 * 												如果有事务,那么加入事务,没有的话新建一个(
	 *                                               不写的情况下) @Transactional(
	 *                                               propagation=Propagation.
	 *                                               REQUIRED)
	 * 
	 *                                               容器不为这个方法开启事务
	 * @Transactional(propagation=Propagation.NOT_SUPPORTED)
	 * 
	 * 														不管是否存在事务,都创建一个新的事务,
	 *                                                       原来的挂起,新的执行完毕,
	 *                                                       继续执行老的事务
	 * @Transactional(propagation=Propagation.REQUIRES_NEW)
	 * 
	 * 														必须在一个已有的事务中执行,
	 *                                                      否则抛出异常 @Transactional
	 *                                                      (propagation=
	 *                                                      Propagation.
	 *                                                      MANDATORY)
	 * 
	 *                                                      必须在一个没有的事务中执行,否则抛出异常
	 *                                                      (与Propagation.
	 *                                                      MANDATORY相反) @Transactional
	 *                                                      (propagation
	 *                                                      =Propagation.NEVER)
	 * 
	 *                                                      如果其他bean调用这个方法,
	 *                                                      在其他bean中声明事务,那就用事务.
	 *                                                      如果其他bean没有声明事务,
	 *                                                      那就不用事务. @Transactional(
	 *                                                      propagation=
	 *                                                      Propagation.
	 *                                                      SUPPORTS)
	 * 
	 */
	@Transactional
	public String add(int id, String name) {
		System.out.println("开始调用1");
		System.out.println(number);

		// AdminUserDO user = new AdminUserDO();
		// user.setUser_id("2");
		// user.setPassword("123456");
		// dao.addAdminUser(user);
		//
		// user = new AdminUserDO();
		// user.setUser_id("3");
		// user.setPassword("123456");
		// dao.addAdminUser(user);
		//
		// user = new AdminUserDO();
		// user.setUser_id("4");
		// user.setPassword("123456");
		// dao.addAdminUser(user);
		//
		// delete();

		// test1.add();
		// DynamicDataSource.setMimi();
		// ContextHolder.setMimiDB();
		add3();
		add2();
		add1();
		System.out.println(1111);
		try {
			// test1.delete();
		} catch (Exception e) {
			// delete();
		}
		System.out.println(333333);
		number++;

		// DynamicDataSource.setManager();
		// ContextHolder.setManagerDB();
		// testStudent();

		// ContextHolder.setMimiDB();
		add4();

		int i = 0;
		int n = 10;
		// int r = n / i;
		System.out.println("结束调用1");

		Adminlog log = new Adminlog();
		
		log.setAction("test");
		log.setLogId(111L);
		log.setUserId("testttt");
		
		logDao.insert(log);

		return "dssssssstest";
	}

	@Transactional
	public void testStudent() {
		List<StudentDO> s = studentDao.getStudents();

		StudentDO s1 = new StudentDO();
		s1.age = 99;
		s1.name = "testhouge";
		s1.sex = 20;
		s1.cdate = new Date();

		studentDao.addStudent(s1);

		studentDao.deleteStudent("1");
	}

	public void add1() {
		AdminUserDO user = new AdminUserDO();
		user.setUser_id("2");
		user.setPassword("123456");
		adminDao.addAdminUser(user);
	}

	public void add2() {
		AdminUserDO user = new AdminUserDO();
		user.setUser_id("3");
		user.setPassword("123456");
		adminDao.addAdminUser(user);
	}

	public void add3() {
		AdminUserDO user = new AdminUserDO();
		user.setUser_id("4");
		user.setPassword("123456");
		adminDao.addAdminUser(user);
	}

	public void add4() {
		AdminUserDO user = new AdminUserDO();
		user.setUser_id("5");
		user.setPassword("123456");
		adminDao.addAdminUser(user);
	}

	@Transactional
	public void delete() {
		adminDao.deleteUser("2");
		adminDao.deleteUser("3");
	}

	public void update() {
		// System.out.println(number++);

		add(1, "20");

		int i = 0;
		int n = 10;
		i++;

		AdminUserDO user = new AdminUserDO();
		user.setId(1);
		user.setPassword("9999999");
		adminDao.updateUser(user);
		// System.out.println(i);

		delete();
	}

}
